package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.mapper.FilesMapper;
import com.example.springboot.entity.Files;
import com.example.springboot.service.IFilesService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;


/**
 * 文件上传相关接口
 */
@RestController
@RequestMapping("/file")
public class FilesController {

    @Resource
    private FilesMapper filesMapper;

    @Resource
    private IFilesService filesService;

    /**
     * 文件上传接口
     *
     * @param file 前端传递过来的文件
     * @return
     */
    @CrossOrigin(origins = {"https://mindfall.cn/","https://www.mindfall.cn/"})
    @PostMapping("/upload")
    public String upload(@RequestParam MultipartFile file) throws IOException {
        return filesService.upload(file);
    }

    /**
     * 文件下载接口   http://localhost:9090/file/{fileUUID}
     * @param fileUUID
     * @param response
     * @throws IOException
     */
    @GetMapping("/{fileUUID}")
    public void download(@PathVariable String fileUUID, HttpServletResponse response) throws IOException {
        filesService.download(fileUUID,response);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Files files) {
        return Result.success(filesMapper.updateById(files));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return filesService.delete(id);
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return filesService.deleteBatch(ids);
    }


}

